package com.atguigu.chapter02;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * TODO 无界流 Wordcount： 消息队列（Kafka）、socket
 *
 * @author cjp
 * @version 1.0
 * @date 2021/3/2 10:01
 */
public class Flink03_WC_UnBoundedStream {
    public static void main(String[] args) throws Exception {
        // 1.获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // 2.读取数据
//        DataStreamSource<String> socketDS = env.socketTextStream("localhost", 9999);
        DataStreamSource<String> socketDS = env.socketTextStream("hadoop1", 9999);

        // 3.处理数据
        // 3.1 压平：切分
        SingleOutputStreamOperator<String> wordDS = socketDS.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String value, Collector<String> out) throws Exception {
                String[] words = value.split(" ");
                for (String word : words) {
                    out.collect(word);
                }
            }
        });
        // 3.2 转换成二元组（word，1）
        SingleOutputStreamOperator<Tuple2<String, Long>> wordAndOneDS = wordDS
//                .map(new MapFunction<String, Tuple2<String, Long>>() {
//                    @Override
//                    public Tuple2<String, Long> map(String value) throws Exception {
//                        return Tuple2.of(value, 1L);
//                    }
//                });
                .map(value -> Tuple2.of(value, 1L))
                .returns(Types.TUPLE(Types.STRING, Types.LONG));

        // 3.3 按照word分组
        KeyedStream<Tuple2<String, Long>, Tuple> wordAndOneKS = wordAndOneDS.keyBy(0);

        // 3.4 组内求和
        SingleOutputStreamOperator<Tuple2<String, Long>> resultDS = wordAndOneKS.sum(1);

        // 4. 输出
        resultDS.print();

        // 5. 启动
        env.execute();

    }


}
